<!-- 登录页 -->
<script setup>
import { ref } from 'vue';
const loginForm = ref({
  username: '',
  password: '',
  agree: false
})

// 规则数据对象
const rules = {
  username: [
    { required: true, message: '用户名为空',trigger: 'blur' }
  ],
  password: [
    { required: true, message: '密码为空' },
    { min: 6, max: 24, message: '密码长度要求6-14个字符',trigger: 'blur' }
  ],
  agree: [
    {
      validator: (rule, val, callback) => {
        return val ? callback() : new Error('请先同意协议')
      }
    }
  ]
}
//登录按钮触发
import { useUserStore } from '@/stores/user';
const userStroe = useUserStore();
const formRef = ref(null)
import router from '@/router';
import {userLogin} from "@/apis/User.js";
import {ElMessage} from "element-plus";
const handleLogin = async () => {
  try {
    // 先验证表单
    await formRef.value.validate()

    // 获取真实数据
    const formData = loginForm.value

    // 提交登录请求
    await userStroe.login(formData)
    const res = userStroe.userInfo
    if (res) {
      // 登录成功
      router.push('/')
    } else {
      // 登录失败（但不是异常）
      ElMessage.error( '用户名或密码错误')
    }
  } catch (error) {
    // 真正的网络异常或其他错误
    if (error.response) {
      // 服务器返回了错误响应，但不在我们预期的 res.code 判断中
      ElMessage.error( '登录失败')
    } else {
      // 网络中断等情况
      ElMessage.error('网络异常，请检查网络连接')
    }

    console.error('登录出错:', error)
  }
}

</script>


<template>
  <div class="login-page">
    <header class="login-header">
      <div class="container">
        <h1 class="logo">
          <RouterLink to="/">
            <img src="@/assets/images/lv-logo.jpg" alt="Logo" />
          </RouterLink>
        </h1>
        <RouterLink class="entry" to="/">进入网站首页</RouterLink>
      </div>
    </header>

    <section class="login-section">
      <div class="left-image">
        <img src="@/assets/images/bg-login.jpg" alt="Travel Background" />
      </div>
      <div class="right-form">
        <div class="wrapper">
          <h2>账户登录</h2>
          <el-form ref="formRef" :rules="rules" :model="loginForm" label-width="0" status-icon>
            <el-form-item prop="account">
              <el-input v-model="loginForm.username" placeholder="账号" />
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="loginForm.password" type="password" placeholder="密码" />
            </el-form-item>
            <el-form-item prop="agree">
              <el-checkbox v-model="loginForm.agree">我已同意隐私条款和服务条款</el-checkbox>
            </el-form-item>
            <el-button type="primary" class="login-btn" @click="handleLogin">点击登录</el-button>
          </el-form>
        </div>
      </div>
    </section>

    <footer class="login-footer">
      <div class="container">
        <p>
          <a href="javascript:;">关于我们</a>
          <a href="javascript:;">帮助中心</a>
          <a href="javascript:;">售后服务</a>
          <a href="javascript:;">配送与验收</a>
        </p>
        <p>Copyright © 履程</p>
      </div>
    </footer>
  </div>
</template>

<style scoped lang="scss">
$dark-bg: #1a1f2e; // Deep charcoal
$accent: #d4a373; // Warm bronze
$light-gray: #e0e0e0; // Subtle light gray
$shadow: rgba(0, 0, 0, 0.3);

.login-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: linear-gradient(135deg, $dark-bg, darken($dark-bg, 10%));
  font-family: 'Playfair Display', serif; // Elegant serif font
  color: $light-gray;
}

.login-header {
  padding: 15px 30px;
  background: rgba($dark-bg, 0.9); // Slightly transparent for depth
  border-bottom: 1px solid rgba($light-gray, 0.1);

  .container {
    max-width: 1200px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .logo img {
    height: 50px;
    width: 50px;
    border-radius: 15px; // Soft rounded corners
    box-shadow: 0 6px 15px $shadow; // Enhanced shadow
    transition: transform 0.3s;

    &:hover {
      transform: scale(1.05); // Slight zoom on hover
    }
  }

  .entry {
    font-size: 16px;
    color: $light-gray;
    text-decoration: underline;
    position: relative;
    transition: color 0.3s;

    &:hover {
      color: $accent;
    }

    &::after {
      content: '»'; // Elegant arrow
      margin-left: 8px;
      font-size: 18px;
      transition: transform 0.3s;
    }

    &:hover::after {
      transform: translateX(5px); // Arrow moves right on hover
    }
  }
}

.login-section {
  flex: 1;
  display: flex;
  max-width: 1000px;
  margin: 30px auto;
  min-height: 600px;

  .left-image {
    flex: 1.2;
    position: relative;
    min-width: 350px;

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      filter: brightness(0.7); // Darken to match theme
    }

    .overlay-text {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      color: $accent;
      font-size: 60px;
      font-weight: 700;
      text-shadow: 3px 3px 6px $shadow;
      letter-spacing: 2px;
    }
  }

  .right-form {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 30px;
  }

  .wrapper {
    width: 100%;
    max-width: 340px;
    background: rgba(255, 255, 255, 0.05);
    backdrop-filter: blur(10px);
    padding: 25px;
    border-radius: 12px;
    box-shadow: 0 8px 20px $shadow;
    border: 1px solid rgba($light-gray, 0.1);

    h2 {
      text-align: center;
      font-size: 24px;
      color: $accent;
      margin-bottom: 25px;
      text-transform: uppercase;
      letter-spacing: 1px;
    }
  }

  .el-form-item {
    margin-bottom: 20px;
  }

  .el-input input {
    height: 45px;
    border-radius: 8px;
    border: 1px solid rgba($light-gray, 0.2);
    background: rgba(255, 255, 255, 0.1);
    color: $light-gray;
    padding-left: 15px;
    transition: all 0.3s;

    &::placeholder {
      color: rgba($light-gray, 0.7);
    }

    &:focus {
      border-color: $accent;
      box-shadow: 0 0 8px rgba($accent, 0.4);
    }
  }

  .el-checkbox {
    color: $light-gray;
    margin-bottom: 20px;

    .el-checkbox__label {
      font-size: 14px;
    }

    .el-checkbox__input.is-checked .el-checkbox__inner {
      background: $accent;
      border-color: $accent;
    }
  }

  .login-btn {
    width: 100%;
    height: 45px;
    background: $accent;
    border-color: $accent;
    color: $dark-bg;
    font-size: 16px;
    border-radius: 8px;
    text-transform: uppercase;
    font-weight: 600;
    transition: all 0.3s;

    &:hover {
      background: lighten($accent, 10%);
      transform: translateY(-2px);
      box-shadow: 0 4px 15px rgba($accent, 0.5);
    }
  }
}

.login-footer {
  padding: 20px 0;
  background: transparent;
  text-align: center;
  border-top: 1px solid rgba($light-gray, 0.1);

  .container {
    max-width: 1200px;
    margin: 0 auto;
  }

  p {
    color: rgba($light-gray, 0.8);
    font-size: 12px;
    margin: 5px 0;

    a {
      color: rgba($light-gray, 0.8);
      text-decoration: none;
      margin: 0 15px;
      transition: color 0.3s;

      &:not(:last-child) {
        border-right: 1px solid rgba($light-gray, 0.3);
        padding-right: 15px;
      }

      &:hover {
        color: $accent;
      }
    }
  }
}

/* Responsive Design */
@media (max-width: 768px) {
  .login-section {
    flex-direction: column;
    min-height: 450px;
  }

  .left-image {
    height: 250px;
    min-width: auto;
  }

  .right-form {
    padding: 15px;
  }

  .wrapper {
    max-width: 100%;
    padding: 20px;
  }

  .login-header .container {
    flex-direction: column;
    align-items: flex-start;
  }

  .login-header .logo {
    margin-bottom: 15px;
  }
}
</style>